package com.leapp.sk.view
import android.app.Activity
import android.content.Context
import android.graphics.drawable.AnimationDrawable
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import com.example.baselibrary.util.SKPermissionUtils
import com.leapp.sk.R
import com.leapp.sk.app.SKApplication
import com.leapp.sk.util.im.LKVoiceRecorder
import com.leapp.sk.util.im.listener.LKVoiceRecorderListener
import java.util.*

/**
 * 作业录音
 * @author  chaohaipeng  on 2018/8/2.
 */
class TaskRecordSoundView :RelativeLayout {


    /**
     * 停止录音父布局
     */
    private  var ll_atrs_stop_record:LinearLayout?=null
    /**
     * 开始录音父布局
     */
    private  var ll_atrs_start_record:LinearLayout?=null
    /**
     * 录音动画
     */
    private  var iv_astr_voice_tag:ImageView?=null
    /**
     * 录制时间
     */
    private  var tv_atrs_record_time:TextView?=null

    /**
     * 开始录音按钮
     */
    private  var iv_ltrs_recording:ImageView?=null
    /**
     * 停止录音按钮
     */
    private  var iv_ltrs_stop_record:ImageView?=null
    /**
     * 录音
     */
    private  var mVoiceRecorder:LKVoiceRecorder?=null
    /**
     * 标记时间
     */
    private var mDuration = 0
    constructor(context: Context) : super(context) {
        init(context)
    }
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        init(context)
    }

    /**
     * 初始化布局
     */
    fun init(context: Context) {
        val view = LayoutInflater.from(context).inflate(R.layout.layout_task_record_sound, this)
        ll_atrs_stop_record= view.findViewById(R.id.ll_atrs_stop_record)
         ll_atrs_start_record= view.findViewById(R.id.ll_atrs_start_record)
        iv_astr_voice_tag=view.findViewById(R.id.iv_astr_voice_tag)
        tv_atrs_record_time=view.findViewById(R.id.tv_atrs_record_time)
        iv_ltrs_recording=view.findViewById(R.id.iv_ltrs_recording)
        iv_ltrs_stop_record=view.findViewById(R.id.iv_ltrs_stop_record)

    }

    /**
     * 初始化事件
     */
   fun initEvent(activity: Activity, mLKVoiceRecorderListener:LKVoiceRecorderListener){
        mVoiceRecorder = LKVoiceRecorder(mLKVoiceRecorderListener)
        iv_ltrs_stop_record?.setOnClickListener(View.OnClickListener {
            val requestAudio = SKPermissionUtils.getInstance().requestAudio(activity)
            if (requestAudio){
                startRecorder()
            }
        })
        iv_ltrs_recording?.setOnClickListener(View.OnClickListener {
            stopRecorder()
        })
   }

    /**
     * 开始录音
     */
    fun startRecorder(){
        ll_atrs_start_record?.visibility=View.VISIBLE
        ll_atrs_stop_record?.visibility=View.GONE
        iv_astr_voice_tag?.visibility=View.VISIBLE
        iv_astr_voice_tag?.setImageResource(R.drawable.animation_imvoice_re)
        val animationDrawable = iv_astr_voice_tag?.drawable as AnimationDrawable
        animationDrawable?.start()
        mVoiceRecorder?.startRecording()
    }

    /**
     * 初始化语音录制时时间显示
     */
     fun initDuration(mDuration:Int) {
        SKApplication.getInstance()?.mHandler?.post(Runnable {
            val durationStr: String
            if (mDuration < 10) {
                durationStr = String.format(Locale.CHINESE, "00:0%d", mDuration)
            } else if (mDuration in 10..59) {
                durationStr = String.format(Locale.CHINESE, "00:%d", mDuration)
            } else {
                durationStr = "01:00"
            }
            tv_atrs_record_time?.text=durationStr
        })

    }

    /**
     * 停止录音
     */
    fun stopRecorder(){
        ll_atrs_start_record?.visibility=View.GONE
        ll_atrs_stop_record?.visibility=View.VISIBLE
        val animationDrawable = iv_astr_voice_tag?.drawable as AnimationDrawable
        animationDrawable?.stop()
        iv_astr_voice_tag?.setImageResource(R.drawable.animation_imvoice_re)
        iv_astr_voice_tag?.visibility=View.GONE
        mVoiceRecorder?.stopRecoding()
    }




}